<?php
require '../model/Db.php';
$Db = Db::getInstance();
$admin_data = $Db->isadmin();
if(!$admin_data){
	$user_data = $Db->islogin();
	if($user_data['job'] == 3){
		$admin_data = $user_data;
	}
}
if(($admin_data || $user_data['job'] != 0)){
	switch ($_GET['type']) {
		case 1: //区版
			$_GET = $Db->escape($_GET);
			$exist = $Db->find([
				table => ['zone'],
				where => "name = '{$_GET['name']}'"
			]);
			if(!$exist){
				$count = $Db->count([
					table => ['zone']
				]);
				$flag = $Db->save([
					table => 'zone',
					map => [
						name => $_GET['name'],
						manager => '{"uids":[]}',
						sort => $count
					]
				]);
			}else{
				$flag = false;
			}
			echo json_encode([res => $flag]);
			break;
		
		case 2://版面
			$_GET = $Db->escape($_GET);
			$exist = $Db->find([
				table => ['module'],
				where => "name = '{$_GET['name']}'"
			]);
			
			if(!$exist){
				$flag = $Db->save([
					table => 'module',
					map => [
						name => $_GET['name'],
						zid => $_GET['id'],
						manager => '{"uids":[]}'
					]
				]);
			}else{
				$flag = false;
			}
			
			echo json_encode([res => $flag]);
			break;

		case 3://种类
			$_GET = $Db->escape($_GET);
			$exist = $Db->find([
				table => ['kind'],
				where => "zid = {$_GET['id']} and name = '{$_GET['name']}'"
			]);
			if(!$exist){
				$flag = $Db->save([
					table => 'kind',
					map => [
						name => $_GET['name'],
						zid => $_GET['id']
					]
				]);
			}else{
				$flag = false;
			}
			
			echo json_encode([res => $flag]);
			break;
	}
}else{
	http_response_code(404);
	echo '<h1 style="color:#337ab7">千呼万唤始出来,犹抱琵琶半遮面</h1>';
}